#!/bin/bash

######字体颜色设置#########
Green_font_prefix="\033[32m" && Red_font_prefix="\033[31m" && Green_background_prefix="\033[42;37m" && Red_background_prefix="\033[41;37m" && Font_color_suffix="\033[0m"
Yello_font_prefix="\033[33m" && Blue_font_prefix="\033[34m" && Pink_font_prefix="\033[35m"
######字体颜色设置#########

sh_ver=1.0

input_ip=">>${Blue_background_prefix}输入需要移除的ip(n:取消)：${Font_color_suffix}\c";
input_port=">>${Blue_background_prefix}输入需要开放的端口(n:取消)：${Font_color_suffix}\c";
input_port_del=">>${Blue_background_prefix}输入需要删除的端口(n:取消)：${Font_color_suffix}\c";
input_operate_type=">>${Blue_background_prefix}选择主菜单功能序号(默认为0)：${Font_color_suffix}\c";
input_init_menu=">>${Blue_background_prefix}是否重新加载主菜单？y/n(默认n)：${Font_color_suffix}\c";

printInfo(){
	 echo -e "${Green_font_prefix}========firewall$sh_ver---Powered By lvjinhui=====${Font_color_suffix}"
	 echo -e "${Red_font_prefix}-1: 退出${Font_color_suffix}"
	 echo -e "${Green_font_prefix}0 :${Font_color_suffix} ${Green_font_prefix}查看被拉黑的ip${Font_color_suffix}"
	 echo -e "${Green_font_prefix}1 :${Font_color_suffix} ${Blue_font_prefix}启动防火墙${Font_color_suffix}"
	 echo -e "${Green_font_prefix}2 :${Font_color_suffix} ${Red_font_prefix}停止防火墙${Font_color_suffix}"
	 echo -e "${Green_font_prefix}3 :${Font_color_suffix} ${Red_font_prefix}重启防火墙${Font_color_suffix}"
	 echo -e "${Green_font_prefix}4 :${Font_color_suffix} ${Yello_font_prefix}设置防火墙开机自启${Font_color_suffix}"
	 echo -e "${Green_font_prefix}5 :${Font_color_suffix} ${Red_font_prefix}刷新防火墙配置${Font_color_suffix}"
	 echo -e "${Green_font_prefix}6 :${Font_color_suffix} ${Yello_font_prefix}把ip从黑名单移除 ${Font_color_suffix}" 
	 echo -e "${Green_font_prefix}7 :${Font_color_suffix} ${Yello_font_prefix}查看日志 ${Font_color_suffix}"
	 echo -e "${Green_font_prefix}8 :${Font_color_suffix} ${Yello_font_prefix}查看连接的ip ${Font_color_suffix}"
	 echo -e "${Green_font_prefix}9 :${Font_color_suffix} ${Blue_font_prefix}放行端口${Font_color_suffix}"
	 echo -e "${Green_font_prefix}10 :${Font_color_suffix} ${Red_font_prefix}删除端口${Font_color_suffix}"
	 echo -e "${Green_font_prefix}11 :${Font_color_suffix} ${Green_font_prefix}查看已放行端口${Font_color_suffix}"
	 echo -e "${Green_font_prefix}========firewall$sh_ver---Powered By lvjinhui=====${Font_color_suffix}"
}


#读取输入信息
inputInfo() {
	echo -e $1
	read -e temp
}

printInfo
 
inputInfo $input_operate_type;

operate_type=$temp;

if [ -z $operate_type ]; then
	operate_type=0
fi

while [ $operate_type -ne -1 ]

do
 #=============================================
 if [ $operate_type -eq 0 ]; then
 	fail2ban-client status sshd
	if [ $? -eq 0 ]; then
		echo -e "${Green_font_prefix}查询成功！！！${Font_color_suffix}"
	else
		echo -e "${Red_font_prefix}查询出错！！！${Font_color_suffix}"
	fi
 fi
 #=================================================
 if [ $operate_type -eq 1 ]; then
	systemctl start fail2ban
	if [ $? -eq 0 ]; then
		echo -e "${Green_font_prefix}启动防火墙成功！！！${Font_color_suffix}"
	else
		echo -e "${Red_font_prefix}启动防火墙出错！！！${Font_color_suffix}"
	fi
 fi
 #=================================================
 if [ $operate_type -eq 2 ]; then
 	systemctl stop fail2ban
	if [ $? -eq 0 ]; then
		echo -e "${Green_font_prefix}停止防火墙成功！！！${Font_color_suffix}"
	else
		echo -e "${Red_font_prefix}停止防火墙出错！！！${Font_color_suffix}"
	fi
 fi
 #=================================================
 if [ $operate_type -eq 3 ]; then
 	systemctl restart fail2ban
	if [ $? -eq 0 ]; then
		echo -e "${Green_font_prefix}重启防火墙成功！！！${Font_color_suffix}"
	else
		echo -e "${Red_font_prefix}重启防火墙出错！！！${Font_color_suffix}"
	fi
 fi
 #=================================================
 if [ $operate_type -eq 4 ]; then
 	systemctl enable fail2ban
	if [ $? -eq 0 ]; then
		echo -e "${Green_font_prefix}设置开机自启成功！！！${Font_color_suffix}"
	else
		echo -e "${Red_font_prefix}设置开机自启出错！！！${Font_color_suffix}"
	fi
 fi
 #=================================================
 if [ $operate_type -eq 5 ]; then
	systemctl daemon-reload
	if [ $? -eq 0 ]; then
		echo -e "${Green_font_prefix}刷新防火墙配置成功！！！${Font_color_suffix}"
	else
		echo -e "${Red_font_prefix}刷新防火墙配置出错！！！${Font_color_suffix}"
	fi
 fi
 #=================================================
 if [ $operate_type -eq 6 ]; then
	inputInfo $input_ip;
	del_ip=$temp;
	
	if [ -z $del_ip ];then
		del_ip="n"
	fi
	if [ $del_ip == "n" ]; then
		echo -e "${Green_font_prefix}取消移除黑名单！！！${Font_color_suffix}"
	else
		fail2ban-client set sshd unbanip $del_ip
		ipset del fail2ban-sshd $del_ip -exist
		if [ $? -eq 0 ]; then
			echo -e "${Green_font_prefix}ip:$del_ip 移除黑名单成功！！！${Font_color_suffix}"
		else
			echo -e "${Red_font_prefix}ip:$del_ip 移除黑名单出错！！！${Font_color_suffix}"
		fi
	fi
	
 fi
 #=================================================
  if [ $operate_type -eq 7 ]; then
    tail /var/log/fail2ban.log
 fi
  #=================================================
  if [ $operate_type -eq 8 ]; then
    netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n
 fi
 #=================================================
 if [ $operate_type -eq 9 ]; then
	inputInfo $input_port;
	add_port=$temp;
	
	if [ -z $add_port ];then
		add_port="n"
	fi
	if [ $add_port == "n" ]; then
		echo -e "${Green_font_prefix}取消添加端口！！！${Font_color_suffix}"
	else
		firewall-cmd --zone=public --add-port=$add_port/tcp --permanent
		firewall-cmd --reload
		if [ $? -eq 0 ]; then
			echo -e "${Green_font_prefix}端口$add_port添加成功！！！${Font_color_suffix}"	
		else
			echo -e "${Red_font_prefix}端口$add_port添加出错！！！${Font_color_suffix}"
		fi
	fi
	
 fi
 
 #=================================================
 if [ $operate_type -eq 10 ]; then
	inputInfo $input_port_del;
	del_port=$temp;
	
	if [ -z $del_port ];then
		del_port="n"
	fi
	if [ $del_port == "n" ]; then
		echo -e "${Green_font_prefix}取消删除端口！！！${Font_color_suffix}"
	else
		firewall-cmd --zone=public --remove-port=$del_port/tcp --permanent
		firewall-cmd --reload
		if [ $? -eq 0 ]; then
			echo -e "${Green_font_prefix}端口$add_port删除成功！！！${Font_color_suffix}"	
		else
			echo -e "${Red_font_prefix}端口$add_port删除出错！！！${Font_color_suffix}"
		fi
	fi
	
 fi
 
  #=================================================
  if [ $operate_type -eq 11 ]; then
    firewall-cmd --zone=public --list-ports
 fi

############################################分割线#########################################################
 
 inputInfo $input_init_menu;
 init_menu=$temp;
 if [ -z $init_menu ];then
	init_menu="n"
 fi
 if [ $init_menu == "y" ]; then
	#重新显示菜单
	printInfo
 fi
 
	
 inputInfo $input_operate_type;
 operate_type=$temp;
 if [ -z $operate_type ]; then
	operate_type=0
 fi
 
done